from typing import TypeVar, IO


_T = TypeVar("_T")


def copy(target: IO[_T], stream: IO[_T], buffer=4096):
    buffer = buffer if buffer > 0 else 4096
    while True:
        c = stream.read(buffer)
        target.write(c)
        if len(c) < buffer:
            break
        target.flush()


def _main():
    with open(input("请输入要合并到的位置（若文件已存在，则在其后追加）："), "ab") as output_file:
        while True:
            path = input("请输入要合并的文件路径（或 E 以结束）：")
            if path.lower() == "e":
                break
            with open(path, "rb") as appending:
                copy(output_file, appending)


if __name__ == "__main__":
    _main()
